package ch01.shili.shili9;

import java.lang.reflect.Field;

public class Person {
    public static void main(String[] args) {
        try {
            Class clz = null;
            clz = Class.forName("reflect.entity.Person");
            Object person = clz.newInstance();

            //获取private String name 属性
            Field name = clz.getDeclaredField("name");

            name.setAccessible(true);
            //先取值看一下
            System.out.println("赋值前的name: " + name.get(person));
            //为name属性赋值
            name.set(person, "New Person");
            //展示效果
            System.out.println("赋值后的name: " + name.get(person));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}
